<?php

class Core_View_Helper_ShortDesc extends Zend_View_Helper_Abstract
{
    protected $_full_decs;
    protected $_short_desc = "";


    public function __construct()
	{
	}

    public function shortDesc($desc, $minLength = 120, $maxLength = 200){
        $this->_full_decs   = $desc;
        
        if(strlen(strip_tags($desc)) > $maxLength){
            $break_pos = 0;
            $cut_string = substr($this->_full_decs, 0, strpos($this->_full_decs, "<br />", $break_pos));
            while (strlen(strip_tags($cut_string)) < $minLength){
                $break_pos++;
                $cut_string = substr($this->_full_decs, 0, strpos($this->_full_decs, "<br />", $break_pos));
            }
            if(strlen(strip_tags($cut_string)) > $maxLength){
                $max_string = substr($cut_string, 0, $maxLength);
                $cut_string = substr($max_string, 0, strrpos($max_string, " "));
            }
            
            $this->_short_desc = '<div class="desc_limit">' . $cut_string . $this->_getShowMore() . '</div>';
            $this->_full_decs = '<div class="desc_full" style="display: none">' . $this->_full_decs . $this->_getShowLess() . '</div>';
            return $this->_short_desc . $this->_full_decs . $this->_getScript();
        } else {
            return $this->_full_decs;
        }
    }
    
    private function _getShowMore()
    {
        $show_more = "... <a href='javascript:void(0);' 
                        class='show_more tooltip_label' 
                        title='" . YouNet_Util::translate('SHOW_MORE_TOOLTIP') . "'>" . 
                            YouNet_Util::translate('SHOW_MORE') .
                    "</a>";
        return $show_more;
    }
    
    private function _getShowLess()
    {
        $show_less = " <a href='javascript:void(0);' 
                        class='show_less tooltip_label' 
                        title='" . YouNet_Util::translate('SHOW_LESS_TOOLTIP') . "'>" . 
                            YouNet_Util::translate('SHOW_LESS') .
                     "</a>";
        return $show_less;
    }
    
    private function _getScript()
    {
        $script = '<script>
                        $(document).ready(function(){
                            $(".show_more").click(function(){
                                $(".desc_limit").hide();
                                $(".desc_full").show();
                            });
                            $(".show_less").click(function(){
                                $(".desc_limit").show();
                                $(".desc_full").hide();
                            });
                        });
                    </script>';
        return $script;
    }
}